<?php

/**
* 上传
*/
if (!function_exists('file_upload')) {

    function file_upload($file_path, $file) {
        $year = date('Y', time());
        $month = date('m', time());
        $day = date('d', time());
        $upload_path = './uploads/images/'.$file_path.'/' . $year . '/' . $month . $day;
        $config = array(
            'upload_path' => $upload_path,
            'file_name' => md5($_FILES[$file]['name'] . time() . rand(1, 10000)),
            'allowed_types' => 'gif|jpg|jpeg|png|jpe',
            'max_size' => '3072',
            'max_width' => '3072',
            'max_height' => '2304',
        );
        if (!file_exists($config['upload_path'])) { //如果目录不存在，就创建它
            mkdir($config['upload_path'], 0777, TRUE);
        }
        $CI = & get_instance();
        $CI->load->library('upload', $config);

        if (!$CI->upload->do_upload($file)) {//上传失败
            $error = $CI->upload->display_errors('', '');
            $CI->form_validation->_error_array[] = $error; //上传错误信息
            return FALSE;
        } else {
            $data = $CI->upload->data();
            $data['path'] = $upload_path . '/' . $data['file_name'];
            return $data;
        }
    }
}